package com.dianrui.agent.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.adapter.ManagerAdapter;
import com.dianrui.agent.bean.Manager;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ManagerActivity extends BaseActivity {
    private List<Manager> managerList=new ArrayList<>();
    private ManagerAdapter managerAdapter;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;



    @Override
    public int getContentViewId() {
        return R.layout.activity_manager_list;
    }

    @Override
    public void initView() {
        title.setText("管理人员");
        managerAdapter=new ManagerAdapter(ManagerActivity.this,managerList);
        listview.setAdapter(managerAdapter);
        getManager();
        managerAdapter.setOnManagerClickListener(new ManagerAdapter.ManagerClickListener() {
            @Override
            public void onManagerClick(String mobile) {
                if (Build.VERSION.SDK_INT >= 23) {
                    if (ContextCompat.checkSelfPermission(ManagerActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                        Intent intent = new Intent(Intent.ACTION_CALL);
                        Uri data = Uri.parse("tel:" + mobile);
                        intent.setData(data);
                        startActivity(intent);
                    } else {
                        ActivityCompat.requestPermissions(ManagerActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
                    }
                } else {
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    Uri data = Uri.parse("tel:" + mobile);
                    intent.setData(data);
                    startActivity(intent);
                }

            }
        });


    }

    private void getManager() {
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl()+ Url.MANAGER, "",  new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)&&!isFinishing()) {
                    try {
                        JSONObject jsonObject=new JSONObject(result);
                        if (managerList != null) {
                            managerList.clear();

                        }
                        switch (jsonObject.optString("status")){
                            case "1000":
                                JSONArray data=jsonObject.optJSONArray("data");
                                if (data.length()>0) {
                                    for (int i = 0; i <data.length() ; i++) {
                                        Manager manager=new Manager();
                                        manager.admin_id=data.optJSONObject(i).optString("admin_id");
                                        manager.account=data.optJSONObject(i).optString("account");
                                        manager.mobile=data.optJSONObject(i).optString("mobile");
                                        manager.role_id=data.optJSONObject(i).optString("role_id");
                                        manager.role_name=data.optJSONObject(i).optString("role_name");
                                        managerList.add(manager);
                                    }
                                    if (managerAdapter != null) {
                                        managerAdapter.refresh(managerList);
                                    }

                                }
                                break;
                            case "1001":
                                ToastUtil.showToast(jsonObject.optString("message"));
                                break;
                            case "1002":
                                spUtils.clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpActivitys(LoginActivity.class);
                                finish();
                                break;

                                default:
                                    break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (managerList.size()<=0) {
                        emptyView.setVisibility(View.VISIBLE);
                        listview.setVisibility(View.GONE);
                    }else {
                        emptyView.setVisibility(View.GONE);
                        listview.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (managerList != null) {
            managerList.clear();

        }
    }
}
